<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="1.2 类加载机制 1.2.1 编译1javac JVMTest1.java  结果产生了两个 class 文件，如下所示： JVMTest1.class 12345678910111213141516&#x2F;&#x2F;&#x2F;&#x2F; Source code recreated from a .class file by IntelliJ IDEA&#x2F;&#x2F; (powered by Fernflower decompiler)">
<meta property="og:type" content="article">
<meta property="og:title" content="JVM-类加载机制">
<meta property="og:url" content="http://littlefxc.github.io/0202/02/23/docs/JVM/JVM-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="1.2 类加载机制 1.2.1 编译1javac JVMTest1.java  结果产生了两个 class 文件，如下所示： JVMTest1.class 12345678910111213141516&#x2F;&#x2F;&#x2F;&#x2F; Source code recreated from a .class file by IntelliJ IDEA&#x2F;&#x2F; (powered by Fernflower decompiler)">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B%E8%AF%A6%E8%A7%A3.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210224212914755.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210809233815237.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210809234920524.png">
<meta property="article:published_time" content="0202-02-23T12:49:02.000Z">
<meta property="article:modified_time" content="2022-05-10T01:24:31.245Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="jvm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B%E8%AF%A6%E8%A7%A3.png">


<link rel="canonical" href="http://littlefxc.github.io/0202/02/23/docs/JVM/JVM-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>JVM-类加载机制 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6"><span class="nav-text">1.2 类加载机制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-1-%E7%BC%96%E8%AF%91"><span class="nav-text">1.2.1 编译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-2-%E5%8A%A0%E8%BD%BD"><span class="nav-text">1.2.2 加载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-3-%E9%93%BE%E6%8E%A5"><span class="nav-text">1.2.3 链接</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-1-%E9%AA%8C%E8%AF%81"><span class="nav-text">1.2.3.1 验证</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-2-%E5%87%86%E5%A4%87"><span class="nav-text">1.2.3.2 准备</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-3-%E8%A7%A3%E6%9E%90"><span class="nav-text">1.2.3.3 解析</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-4-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">1.2.4 初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-5-%E4%BD%BF%E7%94%A8"><span class="nav-text">1.2.5 使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-6-%E5%8D%B8%E8%BD%BD"><span class="nav-text">1.2.6 卸载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-7-%E5%B0%8F%E8%8A%82"><span class="nav-text">1.2.7 小节</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E7%BC%96%E8%AF%91%E5%99%A8%E4%BC%98%E5%8C%96"><span class="nav-text">1.3 编译器优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-1-%E5%AD%97%E8%8A%82%E7%A0%81%E6%98%AF%E5%A6%82%E4%BD%95%E8%BF%90%E8%A1%8C%E7%9A%84%EF%BC%9F"><span class="nav-text">1.3.1 字节码是如何运行的？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-2-Hotspot-%E7%9A%84%E5%8D%B3%E6%97%B6%E7%BC%96%E8%AF%91%E5%99%A8-C1"><span class="nav-text">1.3.2 Hotspot 的即时编译器 C1</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-3-Htospot-%E7%9A%84%E5%8D%B3%E6%97%B6%E7%BC%96%E8%AF%91%E5%99%A8-C2"><span class="nav-text">1.3.3 Htospot 的即时编译器 C2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-4-%E5%88%86%E5%B1%82%E7%BC%96%E8%AF%91"><span class="nav-text">1.3.4 分层编译</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-5-%E5%88%86%E5%B1%82%E7%BC%96%E8%AF%91-JVM%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE%E7%A4%BA%E4%BE%8B"><span class="nav-text">1.3.5 分层编译- JVM参数配置示例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-6-%E5%A6%82%E4%BD%95%E6%89%BE%E5%88%B0%E7%83%AD%E7%82%B9%E4%BB%A3%E7%A0%81%EF%BC%9F%E6%80%9D%E8%B7%AF%EF%BC%9F"><span class="nav-text">1.3.6 如何找到热点代码？思路？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-7-Hotspot-%E5%86%85%E7%BD%AE%E7%9A%84%E4%B8%A4%E7%B1%BB%E8%AE%A1%E6%95%B0%E5%99%A8"><span class="nav-text">1.3.7 Hotspot 内置的两类计数器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-8-%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8%E8%AE%A1%E6%95%B0%E5%99%A8%E6%B5%81%E7%A8%8B"><span class="nav-text">1.3.8 方法调用计数器流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-9-%E5%9B%9E%E8%BE%B9%E8%AE%A1%E6%95%B0%E5%99%A8%E6%B5%81%E7%A8%8B"><span class="nav-text">1.3.9 回边计数器流程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-10-%E6%96%B9%E6%B3%95%E5%86%85%E8%81%94"><span class="nav-text">1.3.10 方法内联</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B9%E6%B3%95%E4%BD%93%E8%B6%B3%E5%A4%9F%E5%B0%8F"><span class="nav-text">方法体足够小</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/0202/02/23/docs/JVM/JVM-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JVM-类加载机制
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：0202-02-23 20:49:02" itemprop="dateCreated datePublished" datetime="0202-02-23T20:49:02+08:00">0202-02-23</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:24:31" itemprop="dateModified" datetime="2022-05-10T09:24:31+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="1-2-类加载机制"><a href="#1-2-类加载机制" class="headerlink" title="1.2 类加载机制"></a>1.2 类加载机制</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B%E8%AF%A6%E8%A7%A3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/类加载过程详解.png"></p>
<h3 id="1-2-1-编译"><a href="#1-2-1-编译" class="headerlink" title="1.2.1 编译"></a>1.2.1 编译</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac JVMTest1.java</span><br></pre></td></tr></table></figure>

<p>结果产生了两个 class 文件，如下所示：</p>
<p>JVMTest1.class</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Source code recreated from a .class file by IntelliJ IDEA</span></span><br><span class="line"><span class="comment">// (powered by Fernflower decompiler)</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.fengxuechao.jvm;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JVMTest1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">JVMTest1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] var0)</span> </span>&#123;</span><br><span class="line">        Demo var1 = <span class="keyword">new</span> Demo(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">        var1.printName();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo.class</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Source code recreated from a .class file by IntelliJ IDEA</span></span><br><span class="line"><span class="comment">// (powered by Fernflower decompiler)</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.fengxuechao.jvm;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Demo</span><span class="params">(String var1)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = var1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="keyword">this</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>反编译</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javap -v -p Demo &gt; a.txt</span><br></pre></td></tr></table></figure>

<p>反编译结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 描述信息</span><br><span class="line">Classfile &#x2F;Users&#x2F;fengxuechao&#x2F;WorkSpace&#x2F;IdeaProjects&#x2F;foodie&#x2F;jvm&#x2F;src&#x2F;main&#x2F;java&#x2F;com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo.class</span><br><span class="line">  Last modified 2021-2-24; size 461 bytes</span><br><span class="line">  MD5 checksum 4232b9a7f5bc219b5c632adabd643a3d</span><br><span class="line">  Compiled from &quot;JVMTest1.java&quot;</span><br><span class="line">&#x2F;&#x2F; 描述信息</span><br><span class="line">class com.fengxuechao.jvm.Demo</span><br><span class="line">  minor version: 0</span><br><span class="line">  major version: 52</span><br><span class="line">  flags: ACC_SUPER</span><br><span class="line">&#x2F;&#x2F; 常量池</span><br><span class="line">Constant pool:</span><br><span class="line">   #1 &#x3D; Methodref          #6.#17         &#x2F;&#x2F; java&#x2F;lang&#x2F;Object.&quot;&lt;init&gt;&quot;:()V</span><br><span class="line">   #2 &#x3D; Fieldref           #5.#18         &#x2F;&#x2F; com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo.name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">   #3 &#x3D; Fieldref           #19.#20        &#x2F;&#x2F; java&#x2F;lang&#x2F;System.out:Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">   #4 &#x3D; Methodref          #21.#22        &#x2F;&#x2F; java&#x2F;io&#x2F;PrintStream.println:(Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">   #5 &#x3D; Class              #23            &#x2F;&#x2F; com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo</span><br><span class="line">   #6 &#x3D; Class              #24            &#x2F;&#x2F; java&#x2F;lang&#x2F;Object</span><br><span class="line">   #7 &#x3D; Utf8               name</span><br><span class="line">   #8 &#x3D; Utf8               Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">   #9 &#x3D; Utf8               &lt;init&gt;</span><br><span class="line">  #10 &#x3D; Utf8               (Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">  #11 &#x3D; Utf8               Code</span><br><span class="line">  #12 &#x3D; Utf8               LineNumberTable</span><br><span class="line">  #13 &#x3D; Utf8               printName</span><br><span class="line">  #14 &#x3D; Utf8               ()V</span><br><span class="line">  #15 &#x3D; Utf8               SourceFile</span><br><span class="line">  #16 &#x3D; Utf8               JVMTest1.java</span><br><span class="line">  #17 &#x3D; NameAndType        #9:#14         &#x2F;&#x2F; &quot;&lt;init&gt;&quot;:()V</span><br><span class="line">  #18 &#x3D; NameAndType        #7:#8          &#x2F;&#x2F; name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">  #19 &#x3D; Class              #25            &#x2F;&#x2F; java&#x2F;lang&#x2F;System</span><br><span class="line">  #20 &#x3D; NameAndType        #26:#27        &#x2F;&#x2F; out:Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">  #21 &#x3D; Class              #28            &#x2F;&#x2F; java&#x2F;io&#x2F;PrintStream</span><br><span class="line">  #22 &#x3D; NameAndType        #29:#10        &#x2F;&#x2F; println:(Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">  #23 &#x3D; Utf8               com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo</span><br><span class="line">  #24 &#x3D; Utf8               java&#x2F;lang&#x2F;Object</span><br><span class="line">  #25 &#x3D; Utf8               java&#x2F;lang&#x2F;System</span><br><span class="line">  #26 &#x3D; Utf8               out</span><br><span class="line">  #27 &#x3D; Utf8               Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">  #28 &#x3D; Utf8               java&#x2F;io&#x2F;PrintStream</span><br><span class="line">  #29 &#x3D; Utf8               println</span><br><span class="line">&#x2F;&#x2F; 字段信息</span><br><span class="line">&#123;</span><br><span class="line">  private java.lang.String name;</span><br><span class="line">    descriptor: Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">    flags: ACC_PRIVATE</span><br><span class="line">&#x2F;&#x2F; 方法的信息</span><br><span class="line">  public com.fengxuechao.jvm.Demo(java.lang.String);</span><br><span class="line">    descriptor: (Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">    flags: ACC_PUBLIC</span><br><span class="line">    Code:</span><br><span class="line">      stack&#x3D;2, locals&#x3D;2, args_size&#x3D;2</span><br><span class="line">         0: aload_0</span><br><span class="line">         1: invokespecial #1                  &#x2F;&#x2F; Method java&#x2F;lang&#x2F;Object.&quot;&lt;init&gt;&quot;:()V</span><br><span class="line">         4: aload_0</span><br><span class="line">         5: aload_1</span><br><span class="line">         6: putfield      #2                  &#x2F;&#x2F; Field name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">         9: return</span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line 16: 0</span><br><span class="line">        line 17: 4</span><br><span class="line">        line 18: 9</span><br><span class="line"></span><br><span class="line">  public void printName();</span><br><span class="line">    descriptor: ()V</span><br><span class="line">    flags: ACC_PUBLIC</span><br><span class="line">    Code:</span><br><span class="line">      stack&#x3D;2, locals&#x3D;1, args_size&#x3D;1</span><br><span class="line">         0: getstatic     #3                  &#x2F;&#x2F; Field java&#x2F;lang&#x2F;System.out:Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">         3: aload_0</span><br><span class="line">         4: getfield      #2                  &#x2F;&#x2F; Field name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">         7: invokevirtual #4                  &#x2F;&#x2F; Method java&#x2F;io&#x2F;PrintStream.println:(Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">        10: return</span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line 21: 0</span><br><span class="line">        line 22: 10</span><br><span class="line">&#125;</span><br><span class="line">SourceFile: &quot;JVMTest1.java&quot;</span><br></pre></td></tr></table></figure>

<p>指令参考：<a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/specs/jvms/se8/html/index.html">https://docs.oracle.com/javase/specs/jvms/se8/html/index.html</a></p>
<h3 id="1-2-2-加载"><a href="#1-2-2-加载" class="headerlink" title="1.2.2 加载"></a>1.2.2 加载</h3><ul>
<li><p>JVM 是如何加载 class 文件的呢？</p>
<p>当一个类被创建实例或者被引用到的时候, 如果虚拟机发现之前没有加载过这个类，就会通过类加载器（也就是 ClassLoader ）把 class 文件加载到内存。</p>
<p>在加载的过程中主要做了 3 件事：</p>
<ol>
<li>读取类的二进制流。</li>
<li>把二进制流转为方法区数据结构，并存放到方法区。</li>
<li>最后，在 Java 堆中产生 java.lang.Class 对象。</li>
</ol>
<p>文档参考：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/williamjie/p/11167920.html">描述一下JVM加载class文件的原理机制</a></p>
</li>
</ul>
<h3 id="1-2-3-链接"><a href="#1-2-3-链接" class="headerlink" title="1.2.3 链接"></a>1.2.3 链接</h3><p>class 文件加载完成后，会进入“链接”这个步骤，链接这个步骤又可以分为“验证”、“准备”和“解析”。</p>
<h4 id="1-2-3-1-验证"><a href="#1-2-3-1-验证" class="headerlink" title="1.2.3.1 验证"></a>1.2.3.1 验证</h4><p>验证——顾名思义，就是验证 class 文件是不是符合规范，这里面包含了多个层次的验证，包括：</p>
<ul>
<li><p>文件格式的验证</p>
<ul>
<li>是否以 <code>0xCAFEBABE</code> 开头（可以用 16 进制编辑器打开查看）</li>
<li>版本好是否合理</li>
</ul>
</li>
<li><p>元数据验证</p>
<ul>
<li>是否有父类</li>
<li>是否继承了 final 类（final 类是不能被继承的，如果继承了，那就是有问题）</li>
<li>非抽象类是否实现了所有抽象方法（没有，那就是有问题）</li>
</ul>
</li>
<li><p>字节码验证</p>
<p>字节码的验证是非常复杂的，一个 class 文件能够通过字节码验证并不代表它没有问题，但是如果它没有通过字节码验证，那就一定有问题。</p>
<ul>
<li>运行检查</li>
<li>栈数据类型和操作码操作参数吻合（比如栈空间只有 2 字节，但其实却需要大于 2 字节，此时就认为这个字节码是有问题的）</li>
<li>跳转指令是不是指向合理的位置</li>
</ul>
</li>
<li><p>符号引用验证</p>
<ul>
<li>常量池中描述类是否存在</li>
<li>访问的方法或字段是否存在且有足够的权限</li>
</ul>
</li>
<li><p>如果事先已经确认代码是安全无误的可以在启动的时候用<code>-Xverify:none</code>关闭验证。</p>
</li>
</ul>
<h4 id="1-2-3-2-准备"><a href="#1-2-3-2-准备" class="headerlink" title="1.2.3.2 准备"></a>1.2.3.2 准备</h4><p>如果通过验证发现没有问题的话，就会进入准备环节。</p>
<p>准备环节的作用：</p>
<ul>
<li>为类的静态变量分配内存，初始化为系统的初始值<ul>
<li>final static 修饰的变量：直接赋值为用户定义的值，比如 <code>private final static int value =123</code>，直接赋值 123。</li>
<li>但是 <code>private static int value = 123</code>, 该阶段的值依然是 0。</li>
</ul>
</li>
</ul>
<h4 id="1-2-3-3-解析"><a href="#1-2-3-3-解析" class="headerlink" title="1.2.3.3 解析"></a>1.2.3.3 解析</h4><p>“准备”完成后，就可以进入解析了。</p>
<p>解析的作用：把符号引用转换成直接引用。</p>
<ul>
<li><p>什么是符号引用？</p>
<p>符号引用（Symbolic References）：符号引用以一组符号来描述所引用的目标，符号可以是任何形式的字面量，只要使用时能够无歧义的定位到目标即可。例如，在Class文件中它以CONSTANT_Class_info、CONSTANT_Fieldref_info、CONSTANT_Methodref_info等类型的常量出现。符号引用与虚拟机的内存布局无关，引用的目标并不一定加载到内存中。在Java中，一个java类将会编译成一个class文件。在编译时，java类并不知道所引用的类的实际地址，因此只能使用符号引用来代替。比如org.simple.People类引用了org.simple.Language类，在编译时People类并不知道Language类的实际内存地址，因此只能使用符号org.simple.Language（假设是这个，当然实际中是由类似于CONSTANT_Class_info的常量来表示的）来表示Language类的地址。各种虚拟机实现的内存布局可能有所不同，但是它们能接受的符号引用都是一致的，因为符号引用的字面量形式明确定义在Java虚拟机规范的Class文件格式中。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210224212914755.png" alt="image-20210224212914755"></p>
</li>
<li><p>什么是直接引用？</p>
<p>可以是</p>
<ul>
<li>直接指向目标的指针（比如，指向“类型”【Class对象】、类变量、类方法的直接引用可能是指向方法区的指针）</li>
<li>相对偏移量（比如，指向实例变量、实例方法的直接引用都是偏移量）</li>
<li>一个能间接定位到目标的句柄</li>
</ul>
<p>直接引用是和虚拟机的布局相关的，同一个符号引用在不同的虚拟机实例上翻译出来的直接引用一般不会相同。如果有了直接引用，那引用的目标必定已经被加载入内存中了。</p>
</li>
</ul>
<h3 id="1-2-4-初始化"><a href="#1-2-4-初始化" class="headerlink" title="1.2.4 初始化"></a>1.2.4 初始化</h3><p>解析完成之后，就会进入初始化这个阶段。在这个阶段，JVM首先会执行 <clinit> 方法，clinit 方法由编译器自动收集里面的所有静态变量赋值动作和静态语句块合并而成，也叫类构造器方法</p>
<ul>
<li>初始化的顺序和源文件中的顺序一致</li>
<li>子类的 <clinit> 被调用前，会先调用父类的 <clinit></li>
<li>JVM 会保证 clinit 方法的线程安全性</li>
</ul>
<p>示例1：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public class JVMTest2 &#123;</span><br><span class="line"></span><br><span class="line">    static &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 静态块&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 构造块&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public JVMTest2() &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 构造方法&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 main() 方法&quot;);</span><br><span class="line">        new JVMTest2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">JVMTest2 静态块</span><br><span class="line">JVMTest2 main() 方法</span><br><span class="line">JVMTest2 构造块</span><br><span class="line">JVMTest2 构造方法</span><br></pre></td></tr></table></figure>

<p>示例2:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JVMTest3</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 静态块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 构造块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">JVMTest3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 main() 方法&quot;</span>);</span><br><span class="line">        <span class="keyword">new</span> Sub();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Super</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Super 静态块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Super 构造块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Super</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Super 构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Sub</span> <span class="keyword">extends</span> <span class="title">Super</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub 静态块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub 构造块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Sub</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub 构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">JVMTest3 静态块</span><br><span class="line">JVMTest3 main() 方法</span><br><span class="line">Super 静态块</span><br><span class="line">Sub 静态块</span><br><span class="line">Super 构造块</span><br><span class="line">Super 构造方法</span><br><span class="line">Sub 构造块</span><br><span class="line">Sub 构造方法</span><br></pre></td></tr></table></figure>

<h3 id="1-2-5-使用"><a href="#1-2-5-使用" class="headerlink" title="1.2.5 使用"></a>1.2.5 使用</h3><p>初始化完成之后就可以使用这个类了。</p>
<h3 id="1-2-6-卸载"><a href="#1-2-6-卸载" class="headerlink" title="1.2.6 卸载"></a>1.2.6 卸载</h3><p>但不使用这个类的话，可以把它卸载掉。</p>
<h3 id="1-2-7-小节"><a href="#1-2-7-小节" class="headerlink" title="1.2.7 小节"></a>1.2.7 小节</h3><p>章节 1.2 的图表示的是一般的类加载流程，而事实上类加载的时候并不一定完全按照这个流程走。例如，解析不一定在初始化之前，也有可能在初始化之后去做。</p>
<h2 id="1-3-编译器优化"><a href="#1-3-编译器优化" class="headerlink" title="1.3 编译器优化"></a>1.3 编译器优化</h2><h3 id="1-3-1-字节码是如何运行的？"><a href="#1-3-1-字节码是如何运行的？" class="headerlink" title="1.3.1 字节码是如何运行的？"></a>1.3.1 字节码是如何运行的？</h3><ul>
<li><p>解释执行：由解释器一行一行翻译执行</p>
<ul>
<li>优势在于没有编译的等待时间</li>
<li>性能相对差一些</li>
</ul>
</li>
<li><p>编译执行：把字节码编译成机器码，直接执行机器码</p>
<ul>
<li>运行效率会高很多，一般认为比解释执行快一个数量级</li>
<li>带来了额外的开销</li>
</ul>
</li>
</ul>
<p>那么如何查看自己的java是解释执行还是编译执行呢？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ java -version</span><br><span class="line">java version <span class="string">&quot;1.8.0_251&quot;</span></span><br><span class="line">Java(TM) <span class="function">SE Runtime <span class="title">Environment</span> <span class="params">(build <span class="number">1.8</span><span class="number">.0_251</span>-b08)</span></span></span><br><span class="line"><span class="function">Java <span class="title">HotSpot</span><span class="params">(TM)</span> 64-Bit Server <span class="title">VM</span> <span class="params">(build <span class="number">25.251</span>-b08, mixed mode)</span></span></span><br></pre></td></tr></table></figure>

<p>mixed mode 代表混合执行，部分解释执行、部分编译执行。</p>
<ul>
<li><p>-Xint:设置JVM的执行模式为解释执行模式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ java -Xint -version</span><br><span class="line">java version <span class="string">&quot;1.8.0_251&quot;</span></span><br><span class="line">Java(TM) <span class="function">SE Runtime <span class="title">Environment</span> <span class="params">(build <span class="number">1.8</span><span class="number">.0_251</span>-b08)</span></span></span><br><span class="line"><span class="function">Java <span class="title">HotSpot</span><span class="params">(TM)</span> 64-Bit Server <span class="title">VM</span> <span class="params">(build <span class="number">25.251</span>-b08, interpreted mode)</span></span></span><br></pre></td></tr></table></figure></li>
<li><p>-Xcomp:JVM优先以编译模式运行，不能编译的，以解释模式运行</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ java -Xcomp -version</span><br><span class="line">java version <span class="string">&quot;1.8.0_251&quot;</span></span><br><span class="line">Java(TM) <span class="function">SE Runtime <span class="title">Environment</span> <span class="params">(build <span class="number">1.8</span><span class="number">.0_251</span>-b08)</span></span></span><br><span class="line"><span class="function">Java <span class="title">HotSpot</span><span class="params">(TM)</span> 64-Bit Server <span class="title">VM</span> <span class="params">(build <span class="number">25.251</span>-b08, compiled mode)</span></span></span><br></pre></td></tr></table></figure></li>
<li><p>-Xmixed:以混合模式运行</p>
</li>
</ul>
<p>一般情况下，我们的代码一开始一般由解释器解释执行。但是当虚拟机发现某个方法或代码块的运行特别频繁的时候，就会认为这些代码是<strong>热点代码（如何定位？）</strong>。为了提高热点代码的执行效率，会用即使编译器（也就是JIT）把这些热点代码编译城与本地平台相关的机器码，<strong>并进行各层次的优化（操作系统的不同、CPU架构的不同）</strong></p>
<h3 id="1-3-2-Hotspot-的即时编译器-C1"><a href="#1-3-2-Hotspot-的即时编译器-C1" class="headerlink" title="1.3.2 Hotspot 的即时编译器 C1"></a>1.3.2 Hotspot 的即时编译器 C1</h3><ul>
<li>是一个简单快速的编译器</li>
<li>主要关注局部性的优化</li>
<li>适用于执行时间较短或启动性能有要求的程序。例如。GUI应用对界面启动速度就有一定要求。、</li>
<li>也被称为 Client Compiler</li>
</ul>
<h3 id="1-3-3-Htospot-的即时编译器-C2"><a href="#1-3-3-Htospot-的即时编译器-C2" class="headerlink" title="1.3.3 Htospot 的即时编译器 C2"></a>1.3.3 Htospot 的即时编译器 C2</h3><ul>
<li>是为长期运行的服务器端应用程序做性能调优的编译器</li>
<li>适用于执行时间较长或对峰值性能有要求的程序</li>
<li>也被称为是 Server Compiler</li>
</ul>
<h3 id="1-3-4-分层编译"><a href="#1-3-4-分层编译" class="headerlink" title="1.3.4 分层编译"></a>1.3.4 分层编译</h3><p>从JDK7开始，正式引入了分层编译的概念，可以细分为 5 种编译级别：</p>
<ul>
<li><ol start="0">
<li>解释执行</li>
</ol>
</li>
<li><ol>
<li>简单 C1 编译：会用 C1 编译器进行一些简单的优化，不开启 Profiling（JVM性能监控）</li>
</ol>
</li>
<li><ol start="2">
<li>受限的 C1 编译：仅执行带<strong>方法调用次数</strong>以及<strong>循环回边执行次数</strong>Profiling的 C1 编译</li>
</ol>
</li>
<li><ol start="3">
<li>完全C1编译：会执行带有所有Profiling的C1代码</li>
</ol>
</li>
<li><ol start="4">
<li>C2 编译：使用C2编译器进行优化，该级别会启用一些编译耗时较长的优化，一些情况下会根据性能监控信息进行一些非常激进的性能优化</li>
</ol>
</li>
</ul>
<p>级别越高，应用启动越慢，优化的开销越高，峰值性能也越高。</p>
<h4 id="1-3-5-分层编译-JVM参数配置示例"><a href="#1-3-5-分层编译-JVM参数配置示例" class="headerlink" title="1.3.5 分层编译- JVM参数配置示例"></a>1.3.5 分层编译- JVM参数配置示例</h4><ul>
<li>只想开启 C2：-XX:-TieredCompilation（禁用中间编译层（123层））</li>
<li>只想开启 C1：-XX:+TieredCompilation -XX:TieredStopAtLevel=1</li>
</ul>
<h3 id="1-3-6-如何找到热点代码？思路？"><a href="#1-3-6-如何找到热点代码？思路？" class="headerlink" title="1.3.6 如何找到热点代码？思路？"></a>1.3.6 如何找到热点代码？思路？</h3><ul>
<li><p>基于采样的热点探测</p>
<p>周期性检查各个线程的栈顶，如果发现某一些方法总是出现在各个栈顶，那就说明是热点代码。</p>
</li>
<li><p>基于计数器的热点探测</p>
<p>大致思路是为每一个方法甚至是代码块建立计数器，然后统计执行的次数，如果超过一定的阈值，那就说明它是热点代码。Hotspot虚拟机采用的就是基于计数器的热点探测。</p>
</li>
</ul>
<h3 id="1-3-7-Hotspot-内置的两类计数器"><a href="#1-3-7-Hotspot-内置的两类计数器" class="headerlink" title="1.3.7 Hotspot 内置的两类计数器"></a>1.3.7 Hotspot 内置的两类计数器</h3><ul>
<li><p>方法调用计数器（Invocation Counter）</p>
<p>用于统计方法被调用的次数，在不开启分层编译的情况下，在 C1 编译器下的默认阈值是 1500 次，在 C2 模式下是 10000次。也可以哦那个 -XX:CompileThreshold=X 指定阈值</p>
</li>
<li><p>回边计数器（Back Edge Counter）</p>
<ul>
<li>用于统计一个方法中循环体代码执行的次数，在字节码中遇到控制流向后跳转的指令称为“回边”（Back Edge）。在不开启分层编译的情况下，C1 编译器心爱的默认阈值 13995，C2 默认为 10700，可使用 -XX:OnStackReplacePercentage=X指定阈值</li>
<li>建立回边计数器的主要目的是为了触发 OSR （OnStackReplacement）编译，参考文档（<a target="_blank" rel="noopener" href="https://www.zhihu.com/question/45910849/answer/100636125">https://www.zhihu.com/question/45910849/answer/100636125</a>）</li>
</ul>
</li>
<li><p>当开启分层编译时，JVM会根据当前编译的方法数以及编译线程数来动态调整阈值，-XX:CompileThreshold、-XX:OnStackReplacePercentage 都会失效。</p>
</li>
</ul>
<h3 id="1-3-8-方法调用计数器流程"><a href="#1-3-8-方法调用计数器流程" class="headerlink" title="1.3.8 方法调用计数器流程"></a>1.3.8 方法调用计数器流程</h3><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210809233815237.png" alt="image-20210809233815237"></p>
<p>如果不做任何设置，方法调用次数统计的并不是方法被调用的绝对次数，而是一个相对的执行频，即一段时间之内方法被调用的次数。当超过一定的时间限度，如果方法的调用次数荏苒不足以让它提交给及时编译器编译，那这个方法的调用计数器就会减少一半，这个过程称为方法调用计数器热度的衰减，而这段时间就称为此方法统计的半衰周期。进行热度衰减的动作是在虚拟机进行垃圾手机是顺便进行的，可以使用虚拟机参数-XX:-UseCounterDecay来关闭热度衰减，让方法计数器统计方法调用的绝对次数，这样，只要系统运行时间足够长，绝大部分方法都会被编译成本地代码。另外，可以使用-XX:CounterHalfLifeTime参数设置半衰周期的时间，单位是秒。</p>
<h3 id="1-3-9-回边计数器流程"><a href="#1-3-9-回边计数器流程" class="headerlink" title="1.3.9 回边计数器流程"></a>1.3.9 回边计数器流程</h3><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210809234920524.png" alt="image-20210809234920524"></p>
<h4 id="1-3-10-方法内联"><a href="#1-3-10-方法内联" class="headerlink" title="1.3.10 方法内联"></a>1.3.10 方法内联</h4><ul>
<li><p>什么事方法内联？示例？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">InlineTest1</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">add1</span><span class="params">(<span class="keyword">int</span> x1, <span class="keyword">int</span> x2, <span class="keyword">int</span> x3, <span class="keyword">int</span> x4)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> add2(x1, x2) + add2(x3, x4);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">add2</span><span class="params">(<span class="keyword">int</span> x1, <span class="keyword">int</span> x2)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> x1 + x3;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 内联后</span></span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">addInline</span><span class="params">(<span class="keyword">int</span> x1, <span class="keyword">int</span> x2, <span class="keyword">int</span> x3, <span class="keyword">int</span> x4)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> x1 + x2 + x3 + x4;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>方法内联的条件 -1</p>
<ul>
<li><h2 id="方法体足够小"><a href="#方法体足够小" class="headerlink" title="方法体足够小"></a>方法体足够小</h2></li>
</ul>
</li>
<li></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/jvm/" rel="tag"># jvm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
            </div>
            <div class="post-nav-item">
                <a href="/0202/02/23/docs/JVM/JVM-%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84/" rel="next" title="JVM-内存结构">
                  JVM-内存结构 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
